home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / binaries / Windows / jsdk / src / sun / servlet / apache / ServletGate.java < prev    next >
Encoding:
Java Source  |  1997-07-18  |  6.1 KB  |  209 lines

  1. // ServletGate - a program that runs Servlets
  2. //
  3. // Copyright (c) 1996 Sun Microsystems, Inc.  All Rights reserved
  4. // Permission to use, copy, modify, and distribute this software
  5. // and its documentation for NON-COMMERCIAL purposes and without
  6. // fee is hereby granted provided that this copyright notice
  7. // appears in all copies. Please refer to the file copyright.html
  8. // for further important copyright and licensing information.
  9. //
  10. // SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11. // THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12. // TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13. // PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14. // ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15. // DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  
  17. package sun.servlet.apache;
  18.  
  19. import java.io.*;
  20. import java.util.*;
  21. import javax.servlet.*;
  22. import javax.servlet.http.*;
  23.  
  24. /** A program that runs Servlets.
  25. ** <P>
  26. ** This is a web program in java, but instead of actually serving
  27. ** content it instead turns the web requests into Jeeves-compatible
  28. ** Servlet requests, and then runs the Servlets.
  29. */
  30.  
  31. public abstract class ServletGate implements ServletContext {
  32.  
  33.     protected PrintStream logStream;
  34.     protected Properties servletProperties;
  35.  
  36.     /** Constructor, specified log stream.
  37.     */
  38.     public ServletGate( PrintStream logStream )    {
  39.     this.logStream = logStream;
  40.     servletProperties = new Properties();
  41.     }
  42.  
  43.     /** Constructor, specified log file.
  44.     */
  45.     public ServletGate( File logFile ) throws IOException {
  46.     this( new PrintStream( new FileOutputStream( logFile ) ) );
  47.     }
  48.  
  49.     /** Constructor, default log stream.
  50.     */
  51.     public ServletGate() {
  52.     this( System.err );
  53.     }
  54.  
  55.     /**
  56.      * Load servlet properties from servlet prop file
  57.      */
  58.     protected void loadServletProps(String servletPropFile) {
  59.     File spf = new File(servletPropFile);
  60.     if (spf.exists() && spf.canRead()) {
  61.         try {
  62.             servletProperties.load(new FileInputStream(spf));
  63.             } catch (IOException ioe) {
  64.         log("Could not load servlet properites file");
  65.         ioe.printStackTrace(logStream);
  66.         }
  67.  
  68.     }
  69.     }
  70.  
  71.  
  72.     // Methods from ServletContext.
  73.  
  74.     protected Hashtable servlets = new Hashtable();
  75.  
  76.     /** Gets a servlet by name.
  77.     ** @param name the servlet name
  78.     ** @return null if the servlet does not exist
  79.     */
  80.     public Servlet getServlet( String name ) {
  81.     return (Servlet) servlets.get( name );
  82.     }
  83.  
  84.     /** Enumerates the servlets in this context (server). Only servlets that
  85.     ** are accesible will be returned. This enumeration always includes the
  86.     ** servlet itself.
  87.     */
  88.     public Enumeration getServlets() {
  89.     return servlets.elements();
  90.     }
  91.  
  92.     /** Destroys all currently-loaded servlets.
  93.     */
  94.     public void destroyAllServlets() {
  95.     Enumeration en = servlets.elements();
  96.     while ( en.hasMoreElements() ) {
  97.         Servlet servlet = (Servlet) en.nextElement();
  98.         servlet.destroy();
  99.     }
  100.     servlets.clear();
  101.     }
  102.  
  103.     /** Write information to the servlet log.
  104.     ** @param message the message to log
  105.     */
  106.     public void log( String message ) {
  107.     Date date = new Date( System.currentTimeMillis() );
  108.     logStream.println( "[" + date.toString() + "] " + message );
  109.     }
  110.  
  111.     /** Write information to the servlet log.
  112.      *  @param servlet the servlet to log information about
  113.      *  @param message the message to log
  114.      */
  115.     public void log( Servlet servlet, String message ) {
  116.     log( servlet.toString() + ": " + message );
  117.     }
  118.  
  119.     /** Internal method to write exceptions to log file.
  120.      *  @param Throwable to log the stacktrace of.
  121.      */
  122.     void log( Throwable t) {
  123.     t.printStackTrace(logStream);
  124.     }
  125.  
  126.     /** Applies alias rules to the specified virtual path and returns the
  127.      * corresponding real path.  It returns null if the translation
  128.      * cannot be performed.
  129.      * @param path the path to be translated
  130.      */
  131.     public String getRealPath( String path ) {
  132.     // No mapping.
  133.     return path;
  134.     }
  135.  
  136.     /** Returns the MIME type of the specified file.
  137.      * @param file file name whose MIME type is required
  138.      */
  139.     public String getMimeType( String file ) {
  140.     int lastDot = file.lastIndexOf( '.' );
  141.     int lastSep = file.lastIndexOf( File.separatorChar );
  142.     if (lastDot == -1 || (lastSep != -1 && lastDot < lastSep))
  143.         return "text/plain";
  144.     String extension = file.substring( lastDot + 1 );
  145.     if ( extension.equals( "html" ) || extension.equals( "htm" ) )
  146.         return "text/html";
  147.     if ( extension.equals( "gif" ) )
  148.         return "image/gif";
  149.     if ( extension.equals( "jpg" ) || extension.equals( "jpeg" ) )
  150.         return "image/jpeg";
  151.     if ( extension.equals( "au" ) )
  152.         return "audio/basic";
  153.     if ( extension.equals( "ra" ) || extension.equals( "ram" ) )
  154.         return "audio/x-pn-realaudio";
  155.     if ( extension.equals( "wav" ) )
  156.         return "audio/x-wav";
  157.     if ( extension.equals( "mpg" ) || extension.equals( "mpeg" ) )
  158.         return "video/mpeg";
  159.     if ( extension.equals( "qt" ) || extension.equals( "mov" ) )
  160.         return "video/quicktime";
  161.     if ( extension.equals( "class" ) )
  162.         return "application/octet-stream";
  163.     if ( extension.equals( "ps" ) )
  164.         return "application/postscript";
  165.     if ( extension.equals( "wrl" ) )
  166.         return "x-world/x-vrml";
  167.     return "text/plain";
  168.     }
  169.  
  170.     /** Returns the name and version of the web server under which the servlet
  171.      * is running.
  172.      * Same as the CGI variable SERVER_SOFTWARE.
  173.      */
  174.     public String getServerInfo() {
  175.     return serverName() + " " + serverVersion();
  176.     }
  177.  
  178.     /** 
  179.      * The name of this software.
  180.      */
  181.     public abstract String serverName();
  182.  
  183.     /**
  184.      * The version of this software.
  185.      */
  186.     public abstract String serverVersion();
  187.  
  188.     /**
  189.      *  The URL for this software.
  190.      */
  191.     public abstract String serverUrl();
  192.  
  193.     /**
  194.      *  Writes an address for this software.
  195.      */
  196.     public void writeAddress( OutputStream o ) {
  197.     PrintStream p = new PrintStream( o );
  198.     p.println("<ADDRESS><A HREF=\"" + serverUrl() + "\">" +
  199.           serverName() + " " + serverVersion() + "</A></ADDRESS>" );
  200.     }
  201.  
  202.     /**
  203.      *  Returns a server defined attribute.  Not currently implemented.
  204.      */
  205.     public Object getAttribute( String name ) {
  206.     return null;
  207.     }
  208. }
  209.